package Model.StatisticalReport;

import Service.StatisticalReport.ReportResolverError;
import com.fasterxml.jackson.annotation.JsonFormat;
import net.sf.oval.ConstraintViolation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UploadDetailResponse {

    static public class Violation{
        private String msg;
        private Object validatedObject;
        private String filed; //有效性校验错误域

        public String getMsg() {
            return msg;
        }

        public Object getValidatedObject() {
            return validatedObject;
        }

        public String getFiled() {
            return filed;
        }

        private Violation(ConstraintViolation violation){
            msg = violation.getMessage();
            validatedObject = violation.getValidatedObject();
            filed = violation.getContext().toString();
        }

        static List<Violation> Convert(List<ConstraintViolation> errors){
            List<Violation> res = new ArrayList<>();
            for(ConstraintViolation violation: errors){
                res.add(new Violation(violation));
            }
            return res;
        }
    }


    public enum Status {
        OK(0, "OK"),
        ERROR(1, "错误"),
        TOO_MANY_RESOLVER_ERROR(2, "出现太多无法解析的数据"),
        TOO_MANY_DUPLICATE_ERROR(3, "出现太多重复数据"),
        TOO_MANY_VIOLATION_ERROR(4, "出现太多无效数据");


        private int code;
        private String msg;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        Status(int code, String msg){
            this.code = code;
            this.msg = msg;
        }

        /**
         * 将所有枚举值转化为HashMap
         * @return
         */
        static public Map<Integer, String> getValues(){
            Map<Integer, String> res = new HashMap<>();
            for (Status each : Status.values()) {
                res.put(each.getCode(), each.getMsg());
            }
            return res;
        }
    }


    private List<List<Violation>> validatorError;

    @JsonFormat(pattern="yyyy-MM")
    private List<Report> duplicateError;

    private List<ReportResolverError> resolverErrors;

    private int successCount; //成功插入的记录数

    private int code;

    private String msg;


    public List<List<Violation>> getValidatorError() {
        return validatorError;
    }

    public void setValidatorError(List<List<Violation>> validatorError) {
        this.validatorError = validatorError;
    }

    public List<Report> getDuplicateError() {
        return duplicateError;
    }


    /**
     * 添加数据校验错误
     * @param error
     */
    public void addValidatorError(List<ConstraintViolation> error){
        if(this.validatorError == null){this.validatorError = new ArrayList<>();}
        this.validatorError.add(Violation.Convert(error));
    }

    /**
     * 添加重复错误
     * @param
     */
    public void addDuplicateError(Report detail){
        if(duplicateError == null){duplicateError = new ArrayList<>();}
        duplicateError.add(detail);
    }

    public List<ReportResolverError> getResolverErrors() {
        return resolverErrors;
    }


    public void setResolverErrors(List<ReportResolverError> resolverErrors) {
        this.resolverErrors = resolverErrors;
    }


    /**
     * 添加解析错误
     * @param error
     */
    public void addResolverErrors(ReportResolverError error){
        if(resolverErrors == null){resolverErrors = new ArrayList<>();}
        resolverErrors.add(error);
    }



    public int getSuccessCount() {
        return successCount;
    }

    /**
     * 插入成功的记录条数
     * @param successCount
     */
    public void setSuccessCount(int successCount) {
        this.successCount = successCount;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setStatus(Status status){
        this.code = status.getCode();
        this.msg = status.getMsg();
    }
}
